//
//  DragView.swift
//  MakeMoney
//
//  Created by Fan ZhiQiang on 2024/12/10.
//

import SwiftUI

struct DragView: View {
    var closure: () -> Void
    
    @GestureState private var dragOffset = CGSize.zero
    @State private var offset = CGSize.zero
    @State private var isDraging = false
    var drag: some Gesture {
        DragGesture()
            .updating($dragOffset) { gesture, state, transaction in
                state = gesture.translation
            }
            .onChanged { value in
                isDraging = true
            }
            .onEnded { value in
                isDraging = false
                //print(value.translation)
                offset.width += value.translation.width
                offset.height += value.translation.height
                withAnimation {
                    offset.width = 0
                }
            }
    }
    
    var body: some View {
        RoundedRectangle(cornerRadius: 15)
            .fill(.dragcolor)
            .frame(width: 80, height: 80)
            .overlay {
                ZStack {
                    Circle()
                        .fill(.dragcirclecolor)
                        .frame(width: 65, height: 65)
                    Circle()
                        .fill(.dragcircle2)
                        .frame(width: 55, height: 55)
                    Circle()
                        .fill(.white)
                        .frame(width: 45, height: 45)
                    Text("选项")
                        .font(.subheadline)
                }
            }
            .padding([.trailing, .bottom], 25)
            .opacity(isDraging ? 1 : 0.5)
            .offset(dragOffset)
            .offset(offset)
            .gesture(drag)
            .onTapGesture {
                closure()
            }
    }
}

#Preview {
    DragView() {}
}
